package handler

import (
	"gin-demo/common/result"
	"gin-demo/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type UserHandler interface {
	GetUserById(c *gin.Context)
}

type userHandlerImpl struct {
	userService service.UserService
}

func NewUserHandler(userService service.UserService) UserHandler {
	return &userHandlerImpl{userService: userService}
}

func (u userHandlerImpl) GetUserById(c *gin.Context) {
	id := c.Query("id")
	userId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		_ = c.Error(err)
		return
	}
	user, err := u.userService.GetUserById(userId)
	if err != nil {
		_ = c.Error(err)
		return
	}
	c.JSON(http.StatusOK, result.Success(user))
}
